import time
from datetime import datetime
from typing import Tuple,Union
from loguru import logger
from app.database.db_sqlite import async_db_session
from app.crud import img_crud
from app.crud import station_crud
from app.crud import dev_crud
from app.models import Station,Dev,Img
from app.schemas.img import CVImg
from cv.classify.classify_state import ClassifyState
from cv.classify.classify_th import ClassifyTH


async def storage_cv_info(timestamp:int,station:Station,dev:Dev,short_cfy:ClassifyState,long_cfy:ClassifyTH=None):
    if long_cfy is None:
        cvimg = CVImg(dev_id=dev.id,station_id=station.id,created_at=datetime.fromtimestamp(timestamp),name=timestamp,content_status=short_cfy.state_text)
    else:
        cvimg = CVImg(
            dev_id=dev.id,
            station_id=station.id,
            created_at=datetime.fromtimestamp(timestamp),
            name=timestamp,
            content_status=short_cfy.state_text,
            temperature_set=long_cfy.temp_set_num,
            content_temperature=long_cfy.temp_num,
            content_humidity=long_cfy.humi_num,
            humidity_set=long_cfy.humi_set_num
        )

    async with async_db_session() as db:
        await img_crud.create_raw_img(db,cvimg)

    
# async def create_user(db: AsyncSession, create: CreateUser) -> NoReturn:
#     new_user = User(**create.dict())
#     db.add(new_user)
#     await db.commit()


#     res = await StationDao.get_station_and_dev_by_name(session,name) 
#     station:Station  = res.mappings().first().get('Station')
#     dev:Dev = station.dev
#     return CvInfo(
#        station_id=station.id,
#        dev_id=dev.id,
#        dev_no=dev.dev_no,
#        cv_type=dev.cv_type,
#        dev_name = str(dev.dev_no)+'号_' + dev.type+'_'
#     )
    
